package com.wpre.transaction.controller;

import com.wpre.transaction.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LianBao
 * Date: 2024-11-28
 * Time: 12:56
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private DataSourceTransactionManager dataSourceTransactionManager;

    //事务的定义
    @Autowired
    private TransactionDefinition transactionDefinition;


    @RequestMapping("/registry")
    public String registry(String username, String password) {

        //开启事务
        //保存当前的状态
        TransactionStatus transaction = dataSourceTransactionManager.getTransaction(transactionDefinition);

        userService.registryUser(username, password);
        //提交事务
        dataSourceTransactionManager.commit(transaction);
        //回滚事务
        dataSourceTransactionManager.rollback(transaction);
        return "注册成功";
    }

}
